package com.itheima.controller;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.*;
import com.itheima.service.db.*;
import com.itheima.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class UmsRoleController {
    @DubboReference
    UmsRoleService umsRoleService;
    @DubboReference
    UmsRoleMenuRelationService urmrService;
    @DubboReference
    UmsMenuService umsMenuService;
    @DubboReference
    UmsRoleResourceRelationService urrrService;
    @DubboReference
    UmsResourceService umsResourceService;

    //根据角色名称分页获取角色列表
    @GetMapping("/list")
    public R findByPage(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return R.success(umsRoleService.findByPage(keyword, pageNum, pageSize));
    }

    //添加角色
    @PostMapping("/create")
    public R create(@RequestBody UmsRole umsRole) {
        umsRoleService.create(umsRole);
        return R.success("添加成功");
    }

    //批量删除角色
    @PostMapping("/delete")
    public R deleteByIds(@RequestParam Long[] ids) {
        umsRoleService.deleteByIds(ids);
        return R.success("删除成功");
    }

    //修改角色
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody UmsRole umsRole) {
        umsRoleService.update(umsRole);
        return R.success("修改成功");
    }

    //修改角色状态
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        umsRoleService.updateStatus(id, status);
        return R.success("修改成功");
    }

    //获取所有角色
    @GetMapping("/listAll")
    public R listAll() {
        return R.success(umsRoleService.listAll());
    }

    //获取角色相关菜单
    @GetMapping("/listMenu/{roleId}")
    public R listMenu(@PathVariable Long roleId) {
        //根据角色id从关系表获取菜单ids
        List<UmsRoleMenuRelation> urmrList = urmrService.findByRoleId(roleId);
        List<Long> menuIds = CollUtil.getFieldValues(urmrList, "menuId", Long.class);
        //根据菜单ids获取每个菜单详情数据
        List<UmsMenu> menuList = umsMenuService.findByMenuIds(menuIds);
        return R.success(menuList);
    }

    //给角色分配菜单
    @PostMapping("/allocMenu")
    public R allocMenu(@RequestParam Long[] menuIds, @RequestParam Long roleId) {
        //删除之前的权限
        urmrService.deleteByRoleId(roleId);
        //新增权限
        urmrService.add(menuIds, roleId);
        return R.success("操作成功");
    }

    //获取角色相关资源
    @GetMapping("/listResource/{roleId}")
    public R listResource(@PathVariable Long roleId) {
        //根据角色id从关系表获取资源ids
        List<UmsRoleResourceRelation> urrrList = urrrService.findByRoleId(roleId);
        List<Long> resourceIds = CollUtil.getFieldValues(urrrList, "resourceId", Long.class);
        //根据资源ids获取每个资源详情数据
        List<UmsResource> resourceList = umsResourceService.findByResourceIds(resourceIds);
        return R.success(resourceList);
    }

    //给角色分配资源
    @PostMapping("/allocResource")
    public R allocResource(@RequestParam Long[] resourceIds, @RequestParam Long roleId) {
        //删除之前的资源
        urrrService.deleteByRoleId(roleId);
        //新增资源
        urrrService.add(resourceIds, roleId);
        return R.success("操作成功");
    }
}
